package cn.com.ahao.gateway.service.impl;

import cn.com.ahao.gateway.service.IApiCodeService;
import cn.com.ahao.gateway.vo.dvo.ApiCodeDVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ApiCodeServiceImpl implements IApiCodeService {


    @Override
    public List<ApiCodeDVO> list() {
        List<ApiCodeDVO> list = new ArrayList<>();
        list.add(generate("MS00001", "nacos-demo", "/hello"));
        list.add(generate("MS00002", "nacos-demo", "/hello"));
        list.add(generate("MS00003", "client-demo", "/hello"));
        list.add(generate("MS00004", "client-demo", "/hello"));
        return list;
    }

    @Override
    public ApiCodeDVO getByApiCode(String apiCode) {
        Optional<ApiCodeDVO> first = this.list().stream().filter(item -> item.getApiCode().equals(apiCode)).findFirst();
        if (first.isPresent()) {
            return first.get();
        }
        return null;
    }


    private ApiCodeDVO generate(String apiCode, String systemCode, String url) {
        ApiCodeDVO dvo = new ApiCodeDVO();
        dvo.setApiCode(apiCode);
        dvo.setSystemCode(systemCode);
        dvo.setUrl(url);
        return dvo;
    }
}
